package com.jingdianjichi.wx.controller;

import com.jingdianjichi.wx.handler.WxMsgFactory;
import com.jingdianjichi.wx.handler.WxMsgHandler;
import com.jingdianjichi.wx.util.MessageUtil;
import com.jingdianjichi.wx.util.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/wx")
@Slf4j
public class CallBackController {
    public static final String TOKEN = "ljjichi";

    @Resource
    private WxMsgFactory wxMsgFactory;

    @RequestMapping("test")
    public String test() {
        return "hello world";
    }

    /**
     * 回调消息校验
     *
     * @return
     */
    @GetMapping("callback")
    public String callBack(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        if (log.isInfoEnabled()) {
            log.info("请求参数验证,signature:{},timestamp:{},nonce:{},echostr:{}",
                    signature, timestamp, nonce, echostr);
        }
        String shaStr = SHA1.getSHA1(TOKEN, timestamp, nonce, "");
        if (signature.equals(shaStr)) {
            return echostr;
        } else {
            return "fail";
        }
    }

    @PostMapping(value = "callback", produces = "application/xml;charset=UTF-8")
    public String callBack(@RequestBody String requestBody,
                           @RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam(value = "msg_signature", required = false) String msg_signature) {
        log.info("接收到微信的请求,requestBody:{}", requestBody);
        Map<String, String> msgMap = MessageUtil.parseXml(requestBody);
        String fromUserName = msgMap.get("FromUserName");
        String toUserName = msgMap.get("ToUserName");
        String msgType = msgMap.get("MsgType");
        String event = msgMap.get("Event") == null ? "" : msgMap.get("Event");
        StringBuilder sb = new StringBuilder();
        sb.append(msgType);
        if (!StringUtils.isEmpty(event)) {
            sb.append(".");
            sb.append(event);
        }
        String msgTypeKey = sb.toString();
        WxMsgHandler handlerByMsgType = wxMsgFactory.getHandlerByMsgType(msgTypeKey);
        if(Objects.isNull(handlerByMsgType)){
            return "unknown";
        }
        String msg = handlerByMsgType.dealMsg(msgMap);

        String response = "<xml><ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
                "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
                "<CreateTime>1726060383</CreateTime>\n" +
                "<MsgType><![CDATA[text]]></MsgType>\n" +
                "<Content><![CDATA[" + msg + "]]></Content>\n" +
                "<MsgId>24711240467372829</MsgId>\n" +
                "</xml>";
        return response;
    }

}
